﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Lion.Data;
using Forest.Lion.Domain.Contracts;
using Forest.Lion.Model;
using Forest.Lion.Domain.Models;

namespace Forest.Lion.Domain.Handlers
{
    public class CompanyAppService
    {
        private readonly ICompanyRepository _companyRepository;

        public CompanyAppService(ICompanyRepository companyRepository)
        {
            this._companyRepository = companyRepository;
        }

        #region Update
        public void Update(ICompanyFormModel formModel)
        {
            if (formModel == null)
                throw new ArgumentNullException("company formmodel");

            try
            {
                var entity = _companyRepository.GetCompany(formModel.Id);
                entity.CompanyName = formModel.CompanyName;
                entity.ShortName = formModel.ShortName;
                entity.Shortcut = formModel.Shortcut;
                entity.Industry = formModel.Industry;
                entity.CompanyType = formModel.CompanyType;
                entity.Employees = formModel.Employees;
                entity.BusinessLicense = formModel.BusinessLicense;
                entity.RegisteredCapital = formModel.RegisteredCapital;
                entity.EstablishedOn = formModel.EstablishedOn;
                entity.Overview = formModel.Overview;
                entity.Business = formModel.Business;
                entity.Province = formModel.Province;
                entity.City = formModel.City;
                entity.Address = formModel.Address;
                entity.PostalCode = formModel.PostalCode;
                entity.Contact = formModel.Contact;
                entity.Tel = formModel.Tel;
                entity.Fax = formModel.Fax;
                entity.Mobile = formModel.Mobile;
                entity.Email = formModel.Email;
                entity.WebSite = formModel.WebSite;
                entity.EnableShortName = formModel.EnableShortName;
                entity.TemplateId = formModel.TemplateId;
                entity.LastUpdated = DateTime.Now;

                _companyRepository.Update(entity);
            }
            catch (InvalidOperationException ex)
            {
                throw new BusinessServicesException("", ex);
            }
        } 
        #endregion

        #region GetById
        public Company GetById(Guid companyId)
        {
            try
            {
                return _companyRepository.GetCompany(companyId);
            }
            catch (InvalidOperationException ex)
            {
                throw new BusinessServicesException("UnableToRetrieveReminderExceptionMessage", ex);
            }
        } 
        #endregion

        #region GetByUserName
        public Company GetByUserName(string userName)
        {
            try
            {
                return _companyRepository.GetCompanyByUserName(userName);
            }
            catch (InvalidOperationException ex)
            {
                throw new BusinessServicesException("UnableToRetrieveReminderExceptionMessage", ex);
            }
        } 
        #endregion

        #region Create
        public void Create(IRegisterFormModel registerForm)
        {
            if (registerForm == null)
                throw new ArgumentNullException("register form is null!");

            try
            {
                var registerComany = registerForm.ConvertToEntity();

                registerComany.Id = new Guid();
                registerComany.DateCreated = DateTime.Now;
                registerComany.LastUpdated = DateTime.Now;
                _companyRepository.Create(registerComany);
            }
            catch (InvalidOperationException ex)
            {
                throw new BusinessServicesException("", ex);
            }
        } 
        #endregion
    }
}
